package com.lj.infoisland.controller;

import com.lj.infoisland.common.convention.result.Result;
import com.lj.infoisland.common.convention.result.Results;
import com.lj.infoisland.domain.dto.AdminUserInfo;
import com.lj.infoisland.domain.dto.LoginDTO;
import com.lj.infoisland.domain.dto.RegisterDTO;
import com.lj.infoisland.domain.dto.UserInitInfoDTO;
import com.lj.infoisland.domain.vo.UserInfoVO;
import com.lj.infoisland.service.UsersService;
import com.lj.infoisland.util.UniqueRandomStringGenerator;
import com.lj.infoisland.util.UserContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/*
 * @description 用户控制层
 * @author 龙洁
 * @create 2024-10-21 下午5:15
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    private UsersService usersService;

    @GetMapping("/test")
    public Result<?> test() {
        return Results.success("hello,world!");
    }

    /**
     * 注册
     *
     * @param registerDTO 注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result<?> register(@RequestBody RegisterDTO registerDTO) {
        return usersService.register(registerDTO);
    }

    /**
     * 登录
     *
     * @param loginDto 登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody LoginDTO loginDto) {
        return usersService.login(loginDto);
    }

    /**
     * 获取用户信息
     *
     * @return UserInfoVO
     */
    @GetMapping("/getUserInfo")
    public Result<UserInfoVO> getUserInfo() {
        UserInfoVO user = UserContext.getUser();
        return usersService.getUserInfo(user.getUserUniqueId());
    }

    /**
     * 修改用户信息
     *
     * @param userInfoVO 用户信息
     * @return void
     */
    @PostMapping("/alterUserInfo")
    public Result<Void> alterUserInfo(@RequestBody UserInfoVO userInfoVO) {
        return usersService.alterUserInfo(userInfoVO);
    }

    /**
     * 发送验证码
     *
     * @param phoneNumber 手机号
     * @return void
     */
    @GetMapping("/sendVerification")
    public Result<?> sendVerification(@RequestParam("phoneNumber") String phoneNumber) {
        //生成随机六位字符串
        String code = UniqueRandomStringGenerator.generateRandomChars(6);
        // 绑定redis
        return usersService.setVerificationCode(phoneNumber, code);
    }

    /**
     * 绑定手机号
     *
     * @param phoneNumber 手机号
     * @param code        验证码
     * @return void
     */
    @GetMapping("/bindPhoneNumber")
    public Result<?> bindPhoneNumber(@RequestParam("phoneNumber") String phoneNumber, @RequestParam("code") String code) {
        usersService.alterPhoneNumber(phoneNumber, code);
        return Results.success();
    }

    // 用户信息初始化
    @PostMapping("/initUserInfo")
    public Result<?> initUserInfo(@RequestBody UserInitInfoDTO userInitInfoDTO) {
        return usersService.initUserInfo(userInitInfoDTO);
    }

    /**
     * 修改手机号
     *
     * @param phoneNumber 手机号
     * @param code        验证码
     * @return void
     */
    @PostMapping("/alterPhoneNumber")
    public Result<?> alterPhoneNumber(String phoneNumber, String code) {
        usersService.alterPhoneNumber(phoneNumber, code);
        return Results.success();
    }

    @GetMapping("/getUserList")
    public Result<?> getUserList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 @RequestParam(value = "username") String username, @RequestParam(value = "role") String role){
        return Results.success(usersService.getUserList(pageNum, pageSize,username,role));
    }

    @GetMapping("/getUserTotal")
    public Result<?> getUserTotal(){
        return Results.success(usersService.getUserTotal());
    }

    @PostMapping("/addAdminUser")
    public Result<?> addAdminUser(@RequestBody AdminUserInfo adminUserInfo){
        usersService.addAdminUser(adminUserInfo);
        return Results.success();
    }

    @GetMapping("/updateUserStatus")
    public Result<?> updateUserStatus(@RequestParam("userUniqueId") String userUniqueId,@RequestParam("status") Integer status){
        usersService.updateUserStatus(userUniqueId,status);
        return Results.success();
    }
}
